package cn.yuhh.api.v1.declare;

import cn.yuhh.api.helper.ApiResponse;
import cn.yuhh.api.service.AbstractApiService;
import cn.yuhh.core.annotation.NotNull;
import cn.yuhh.core.domain.DeclareInfo;
import cn.yuhh.core.domain.User;
import cn.yuhh.core.type.Role;
import com.magic.spring.mongo.IMongoUpdate;
import com.magic.spring.mongo.Mongo;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author ysw
 * @create 2020-02-25-16:13
 * @descreption 领导拟审核
 **/
@Component("distApproval1.0")
@NotNull(name = {"id"})
public class DistApprovalService extends AbstractApiService {

    final private static int STEP_2 = 2;
    @Override
    public Map service(Map request) {
        DeclareInfo declareInfo = Mongo.buildMongo().eq("_id", id(request)).findOne(DeclareInfo.class);
        User manager = Mongo.buildMongo().eq("areaInfo.countyCode", declareInfo.getAreaInfo().getCountyCode()).eq("role", Role.EXAMINE_HEADER).findOne(User.class);

        Mongo.buildMongo().eq("_id",id(request)).updateFirst(new IMongoUpdate() {
            @Override
            public void update(Update update) {
                if(manager == null){
                    User managerIssue = Mongo.buildMongo().eq("areaInfo.countyCode", declareInfo.getAreaInfo().getCityCode()) .eq("role",Role.ISSUE_HEADER).findOne(User.class);
                    update.set("mainManagerId", managerIssue.getId());
                }else {
                    update.set("mainManagerId", manager.getId());
                }
                update.set("declareStatus", declareInfo.getDeclareStatus().handler(whether(request)));
                update.set("updateAt", System.currentTimeMillis() / 1000);
            }
        },DeclareInfo.class);

        return ApiResponse.success();
    }
}
